// @author ARISON NEO WEE CHEN
var InputManager = function() {
    var _FPS;
    var _canvas;
    var _scene;

    var _mouse = {
        x : 0,
        y : 0,
        press :false
    }
    var _keys = [];// physical key states
    var K_LEFT = 37;
    var K_RIGHT = 39;
    var K_UP = 38;
    var K_DOWN = 40;
    var K_CTRL = 17;
    var K_SPACE = 32;
    var K_ENTER = 13;
    var K_PUNCH = 70 // F key
    var K_KICK = 68 // D key
    var K_BLOCK = 83  // S key
    var K_JUMP = 32 // space jump
    var K_CAPS = 20;
    var K_1 = 49;
    var K_2 = 50;
    var K_3 = 51;
    var K_4 = 52;
    var K_SPECIAL = 71; // G key, special attack

    var COUNT;
    var DELAY_RATE = 2;

    this.init = function(FPS, canvas) {
        _canvas = canvas;
        _FPS = FPS;
        COUNT = 0;

        _keys = [];
        for (var i=0; i<255; i++) {
            var newKey = {press : false};
            _keys.push(newKey);
        }

        window.addEventListener('keydown', 	 this.handleKeyDown, true );
        window.addEventListener('keyup', 	 this.handleKeyUp, true );
        /*
        window.addEventListener("mousedown", this.clickInCanvas, true );
        window.addEventListener("mouseup", 	 this.releaseInCanvas, true );
        window.addEventListener("mousemove", this.moveInCanvas, true );*/

    };
    this.setScene = function(scene) {
        _scene = scene;
    };

    /////////////////////////////////////////////////////////
    // User-Event interface
    this.clickInCanvas = function( e )
    {
        var x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        var y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;

        x -= _canvas.offsetLeft;
        y -= _canvas.offsetTop;

        if( x >= 0 && y >= 0
            && x < _canvas.width
            && y < _canvas.height )
        {
            _mouse.press = true ;
        }
    };

    this.releaseInCanvas = function( e )
    {
        var x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        var y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;

        x -= _canvas.offsetLeft;
        y -= _canvas.offsetTop;

        if( x >= 0 && y >= 0
            && x < _canvas.width
            && y < _canvas.height )
        {
            _mouse.press = false ;
        }
    };

    this.moveInCanvas = function( e )
    {
        var x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        var y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;

        x -= _canvas.offsetLeft;
        y -= _canvas.offsetTop;

        if( x >= 0 && y >= 0
            && x < _canvas.width
            && y < _canvas.height )
        {
            _mouse.x = x ;
            _mouse.y = y ;
        }
    };

    this.handleKeyDown = function( evt )
    {
        //this.keys[evt.keyCode] = this.keys[evt.keyCode];
        evt.preventDefault();
        if (evt.keyCode === K_PUNCH)
           var a= 0 ;
        _keys[evt.keyCode].press = true;
    };
    this.handleKeyUp = function( evt )
    {
        //this.keys[evt.keyCode] = this.keys[evt.keyCode];
        _keys[evt.keyCode].press = false;
    };
    this.processEntry = function() {
        if (_keys[K_1].press) {
            setDebugMode();
        }
        if (_keys[K_2].press) {
            clearDebugMode();
        }
        if (_keys[K_3].press) {
            setDebugModeInfo();
        }
        if (_keys[K_4].press) {
            clearDebugModeInfo();
        }
        // actions

        if (1) {
            // delay certain amount to allow time for key combination
            COUNT = (COUNT+1)%DELAY_RATE;
            if (COUNT != 0) {
                return;
            }

            switch(_scene.Player.state){
                case "stillRight":
                    if(_keys[K_UP].press) {
                        _scene.Player.updateState("walkUpRight");
                    }
                    else if(_keys[K_DOWN].press) {
                        _scene.Player.updateState("walkDownRight");
                    }
                    else if(_keys[K_LEFT].press) {
                        _scene.Player.updateState("walkLeft");
                    }
                    else if(_keys[K_RIGHT].press) {
                        _scene.Player.updateState("walkRight");
                    }

                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchRight");
                    }
                    else if(_keys[K_KICK].press && _keys[K_UP].press) {
                        _scene.Player.updateState("kickHighRight");
                    }
                    else if(_keys[K_KICK].press && _keys[K_DOWN].press) {
                        _scene.Player.updateState("kickLowRight");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidRight");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockRight");
                    }
                    else if(_keys[K_SPECIAL].press) {
                        if (_scene.Player.comboCount >= 10) {
                            _scene.Player.updateState("specialLeft");
                            _scene.Player.decreaseCombo(10);
                        }

                    }

                    break;
                case "stillLeft":
                    if(_keys[K_UP].press) {
                        _scene.Player.updateState("walkUpLeft");
                    }
                    else if(_keys[K_DOWN].press) {
                        _scene.Player.updateState("walkDownLeft");
                    }
                    else if(_keys[K_LEFT].press) {
                        _scene.Player.updateState("walkLeft");
                    }
                    else if(_keys[K_RIGHT].press) {
                        _scene.Player.updateState("walkRight");
                    }



                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchLeft");
                    }
                    else if(_keys[K_KICK].press && _keys[K_UP].press) {
                        _scene.Player.updateState("kickHighLeft");
                    }
                    else if(_keys[K_KICK].press && _keys[K_DOWN].press) {
                        _scene.Player.updateState("kickLowLeft");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidLeft");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockLeft");
                    }
                    else if(_keys[K_SPECIAL].press) {
                        if (_scene.Player.comboCount >= 10) {
                            _scene.Player.updateState("specialLeft");
                            _scene.Player.decreaseCombo(10);
                        }
                    }
                    break;
                case "walkDownRight":
                    if(_keys[K_UP].press) {
                        _scene.Player.updateState("walkUpRight");
                    }
                    else if(_keys[K_LEFT].press) {
                        _scene.Player.updateState("walkLeft");
                    }
                    else if(_keys[K_RIGHT].press) {
                        _scene.Player.updateState("walkRight");
                    }

                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchRight");
                    }
                    else if(_keys[K_KICK].press && _keys[K_UP].press) {
                        _scene.Player.updateState("kickHighRight");
                    }
                    else if(_keys[K_KICK].press && _keys[K_DOWN].press) {
                        _scene.Player.updateState("kickLowRight");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidRight");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockRight");
                    }

                    break;
                case "walkDownLeft":
                    if(_keys[K_UP].press) {
                        _scene.Player.updateState("walkUpLeft");
                    }
                    else if(_keys[K_LEFT].press) {
                        _scene.Player.updateState("walkLeft");
                    }
                    else if(_keys[K_RIGHT].press) {
                        _scene.Player.updateState("walkRight");
                    }

                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchLeft");
                    }
                    else if(_keys[K_KICK].press && _keys[K_UP].press) {
                        _scene.Player.updateState("kickHighLeft");
                    }
                    else if(_keys[K_KICK].press && _keys[K_DOWN].press) {
                        _scene.Player.updateState("kickLowLeft");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidLeft");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockLeft");
                    }

                    break;
                case "walkUpRight":
                    if(_keys[K_DOWN].press) {
                        _scene.Player.updateState("walkDownRight");
                    }
                    else if(_keys[K_LEFT].press) {
                        _scene.Player.updateState("walkLeft");
                    }
                    else if(_keys[K_RIGHT].press) {
                        _scene.Player.updateState("walkRight");
                    }

                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchRight");
                    }
                    else if(_keys[K_KICK].press && _keys[K_UP].press) {
                        _scene.Player.updateState("kickHighRight");
                    }
                    else if(_keys[K_KICK].press && _keys[K_DOWN].press) {
                        _scene.Player.updateState("kickLowRight");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidRight");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockRight");
                    }

                    break;
                case "walkUpLeft":
                    if(_keys[K_DOWN].press) {
                        _scene.Player.updateState("walkDownLeft");
                    }
                    else if(_keys[K_LEFT].press) {
                        _scene.Player.updateState("walkLeft");
                    }
                    else if(_keys[K_RIGHT].press) {
                        _scene.Player.updateState("walkRight");
                    }

                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchLeft");
                    }
                    else if(_keys[K_KICK].press && _keys[K_UP].press) {
                        _scene.Player.updateState("kickHighLeft");
                    }
                    else if(_keys[K_KICK].press && _keys[K_DOWN].press) {
                        _scene.Player.updateState("kickLowLeft");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidLeft");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockLeft");
                    }

                    break;
                case "walkRight":
                    if(_keys[K_LEFT].press) {
                        _scene.Player.updateState("walkLeft");
                    }


                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchRight");
                    }
                    else if(_keys[K_KICK].press && _keys[K_UP].press) {
                        _scene.Player.updateState("kickHighRight");
                    }
                    else if(_keys[K_KICK].press && _keys[K_DOWN].press) {
                        _scene.Player.updateState("kickLowRight");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidRight");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockRight");
                    }

                    break;
                case "walkLeft":
                    if(_keys[K_RIGHT].press) {
                        _scene.Player.updateState("walkRight");
                    }

                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchLeft");
                    }
                    else if(_keys[K_KICK].press && _keys[K_UP].press) {
                        _scene.Player.updateState("kickHighLeft");
                    }
                    else if(_keys[K_KICK].press && _keys[K_DOWN].press) {
                        _scene.Player.updateState("kickLowLeft");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidLeft");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockLeft");
                    }

                    break;
                case "punchLeft":
                    if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidLeft");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockLeft");
                    }

                    break;
                case "punchRight":
                    if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidRight");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockRight");
                    }

                    break;
                case "kickLowLeft":
                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchLeft");
                    }

                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockLeft");
                    }

                    break;
                case "kickLowRight":
                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchRight");
                    }

                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockRight");
                    }

                    break;
                case "kickMidLeft":
                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchLeft");
                    }

                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockLeft");
                    }

                    break;
                case "kickMidRight":
                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchRight");
                    }

                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockRight");
                    }

                    break;
                case "kickHighLeft":
                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchLeft");
                    }

                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockLeft");
                    }

                    break;
                case "kickHighRight":
                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchRight");
                    }

                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }
                    else if(_keys[K_BLOCK].press) {
                        _scene.Player.updateState("blockRight");
                    }

                    break;
                case "blockLeft":
                    if(_keys[K_UP].press) {
                        _scene.Player.updateState("walkUpLeft");
                    }
                    else if(_keys[K_DOWN].press) {
                        _scene.Player.updateState("walkDownLeft");
                    }
                    else if(_keys[K_LEFT].press) {
                        _scene.Player.updateState("walkLeft");
                    }
                    else if(_keys[K_RIGHT].press) {
                        _scene.Player.updateState("walkRight");
                    }

                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchLeft");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidLeft");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }

                    if(_keys[K_BLOCK].press) {
                        _scene.Player.blocking = true;
                    }
                    else
                        _scene.Player.blocking = false;

                    break;
                case "blockRight":
                    if(_keys[K_UP].press) {
                        _scene.Player.updateState("walkUpRight");
                    }
                    else if(_keys[K_DOWN].press) {
                        _scene.Player.updateState("walkDownRight");
                    }
                    else if(_keys[K_LEFT].press) {
                        _scene.Player.updateState("walkLeft");
                    }
                    else if(_keys[K_RIGHT].press) {
                        _scene.Player.updateState("walkRight");
                    }

                    if(_keys[K_PUNCH].press) {
                        _scene.Player.updateState("punchRight");
                    }
                    else if(_keys[K_KICK].press) {
                        _scene.Player.updateState("kickMidRight");
                    }
                    else if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }

                    if(_keys[K_BLOCK].press) {
                        _scene.Player.blocking = true;
                    }
                    else
                        _scene.Player.blocking = false;

                    break;
                case "jumpLeft":
                    /*if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpLeft");
                    }
*/
                    break;
                case "jumpRight":
                   /* if(_keys[K_JUMP].press) {
                        _scene.Player.updateState("jumpRight");
                    }
*/
                    break;
                default:
                    break;
    }


      }
}
};
var inputManager = new InputManager();